package com.bing.plugins.jpush;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @function
 * @author fengjing:
 * @date ：2015年11月10日 下午3:00:38
 * @mail 164303256@qq.com
 */
public class Push4JiGuang {

  private JPushClient jPushClient;

  private boolean apnsProduction;

  private static final Logger LOG = LoggerFactory.getLogger(Push4JiGuang.class);


  /**
   * 
   * @Description 消息推送
   * @author 王兴岭
   * @date 2016年7月14日 下午8:03:44
   * @action sendMessage
   * @param deviceType 设备类型 1：android;2:ios
   * @param audience 消息接受者
   * @param message 消息
   * @param extras 额外的参数
   */
  public void sendMessage(int deviceType, String audience, String message,
      Map<String, String> extras) {
    Preconditions.checkNotNull(audience, "audience can't be null");
    Preconditions.checkArgument(deviceType == 1 || deviceType == 2,
        "deviceType: 1 为  android;2 为  ios");
    Preconditions.checkNotNull(message, "message can't be null");


    Options opt = Options.newBuilder().build();
    opt.setApnsProduction(apnsProduction);// true为生产模式，false为开发模式
    Notification notification = null;

    switch (deviceType) {
      case 2:// ios
        IosNotification ios = IosNotification.newBuilder()//
            .setAlert(message)//
            .setContentAvailable(true)//
            .addExtras(extras).build();
        notification = Notification.newBuilder()//
            .addPlatformNotification(ios).build();
        break;
      case 1: // android
        notification = Notification.android(message, message, extras);
        break;
    }
    PushPayload pushPayload = PushPayload.newBuilder()//
        .setPlatform(Platform.all())//
        .setAudience(Audience.alias(audience))//
        .setNotification(notification)//
        .setOptions(opt).build();
    try {
      PushResult result = jPushClient.sendPush(pushPayload);
      LOG.info("Got result - " + result);
    } catch (APIConnectionException | APIRequestException e) {
      LOG.error(e.getMessage());
    }
  }

  /**
   * 
   * @Description 极光推送工具类
   * @author 王兴岭
   * @date 2016年6月7日 上午11:30:17
   * @action sendMessage
   * @param message 推送的消息
   * @param alia 被推送人的id
   * @param type 0订单相关，1租友相关，2小租推送
   * @param _id
   * @param logintype
   * @param status
   */
  @Deprecated
  public void sendMessage(String message, String alia, String type, String _id, String logintype,
      String status) {
    if (alia == null || message == null) {
      return;
    }
    PushPayload payload = getPushPayload(message, alia, type, _id, logintype, status);
    try {
      PushResult sendPush = jPushClient.sendPush(payload);
      System.out.println(sendPush.toString());
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      e.printStackTrace();
    }
  }

  @Deprecated
  public void sendMessage(String message, String alia, String type, String _id, String logintype,
      String status, String orderType) {
    if (alia == null || message == null) {
      return;
    }
    PushPayload payload = getPushPayload(message, alia, type, _id, logintype, status, orderType);
    try {
      jPushClient.sendPush(payload);

    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      e.printStackTrace();
    }
  }

  @Deprecated
  private PushPayload getPushPayload(String message, String alia, String type, String _id,
      String logintype, String status, String orderType) {
    Map<String, String> extras = new HashMap<String, String>();
    extras.put("_id", _id);
    extras.put("type", type);
    extras.put("status", status);
    extras.put("content", message);
    extras.put("orderType", orderType);// 1:约单，2租单
    Options opt = Options.newBuilder().build();
    opt.setApnsProduction(true);// true为生产模式，false为开发模式
    Notification notification = null;
    if (logintype.equals("0")) {
      notification = Notification
          .newBuilder().addPlatformNotification(((IosNotification.Builder) IosNotification
              .newBuilder().setAlert(message).setContentAvailable(true).addExtras(extras)).build())
          .build();
    } else {
      notification = Notification.android(message, message, extras);
    }

    return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alia))
        .setNotification(notification).setOptions(opt).build();
  }

  @Deprecated
  private static PushPayload getPushPayload(String message, String alia, String type, String _id,
      String logintype, String status) {
    Map<String, String> extras = new HashMap<String, String>();
    extras.put("_id", _id);
    extras.put("type", type);
    extras.put("status", status);
    Options opt = Options.newBuilder().build();
    opt.setApnsProduction(false);
    Notification notification = null;
    Builder pushpayload =
        PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alia));
    if (logintype.equals("0")) {
      pushpayload.setOptions(opt);
      notification = Notification.ios(message, extras);
    } else {
      notification = Notification.android(message, message, extras);
    }
    pushpayload.setNotification(notification);
    return pushpayload.build();
  }

  @Deprecated
  public int getPush(String message, String title, String type, String content, String url) {
    Map<String, String> extras = new HashMap<String, String>();
    extras.put("content", content);
    extras.put("type", type);
    extras.put("url", url);
    Options opt = Options.newBuilder().build();
    opt.setApnsProduction(true);
    PushPayload payloadAndroid =
        PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all())
            .setNotification(Notification.android(message, title, extras)).build();
    PushPayload payloadios =
        PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all())
            .setNotification(Notification.ios(content, extras)).setOptions(opt).build();

    try {
      PushResult andResult = jPushClient.sendPush(payloadAndroid);
      PushResult iosResult = jPushClient.sendPush(payloadios);
      return andResult.getResponseCode();
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      e.printStackTrace();
    }
    return 200;
  }

  @Deprecated
  public int getPushtest(String message, String alia, String title, String type, String content,
      String url) {
    Map<String, String> extras = new HashMap<String, String>();
    extras.put("content", content);
    extras.put("type", type);
    extras.put("url", url);
    Options opt = Options.newBuilder().build();
    opt.setApnsProduction(true);
    PushPayload payloadAndroid =
        PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.alias(alia))
            .setNotification(Notification.android(message, title, extras)).build();
    PushPayload payloadios =
        PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.alias(alia))
            .setNotification(Notification.ios(content, extras)).setOptions(opt).build();
    try {
      // PushResult result = jpushClient.sendPush(payloadAndroid);
      PushResult result = jPushClient.sendPush(payloadios);
      return result.getResponseCode();
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      e.printStackTrace();
    }
    return 200;
  }

  public JPushClient getjPushClient() {
    return jPushClient;
  }

  public void setjPushClient(JPushClient jPushClient) {
    this.jPushClient = jPushClient;
  }

  public boolean isApnsProduction() {
    return apnsProduction;
  }

  public void setApnsProduction(boolean apnsProduction) {
    this.apnsProduction = apnsProduction;
  }
}
